package com.sg.business.commons.operation.handler.vault;

import org.eclipse.swt.widgets.Shell;

import com.mobnut.db.model.IPrimaryObjectEventListener;
import com.mobnut.db.model.PrimaryObject;
import com.sg.business.commons.ui.vault.IVaultControlPart;
import com.sg.business.model.Document;
import com.sg.widgets.MessageUtil;
import com.sg.widgets.part.editor.DataObjectEditor;

public class EditDocument extends AbstractDocumentHandler {

	@Override
	protected void run(final Document document,
			final IVaultControlPart vaultControlPart, Shell shell) {
		try {
			document.reload();
		} catch (Exception e1) {
			MessageUtil.showToast(e1);
		}

		if (vaultControlPart != null) {

			document.addEventListener(new IPrimaryObjectEventListener() {
				@Override
				public void primaryObjectEvent(String code, PrimaryObject po) {
					if (IPrimaryObjectEventListener.UPDATED.equals(code)) {
						vaultControlPart.update(document);
					}
				}
			});
		}

		try {
			DataObjectEditor.open(document, document.getDefaultEditorId(),
					true, null);
		} catch (Exception e) {
			MessageUtil.showToast(e);
		}
	}

}
